﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

public partial class Pages_MasterPage : System.Web.UI.MasterPage
{
    // Website pages considered to be "catalog pages" that the visitor
    // can "Continue Shopping" to
    private static string[] catalogPages = { "~/Pages/Default.aspx", "~/Pages/Presents.aspx",
"~/Search.aspx" };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillMainMenu();
            /* Save the latest visited catalog page into the session
to support "Continue Shopping" functionality */
            // Get the currently loaded page
            string currentLocation = Request.AppRelativeCurrentExecutionFilePath;
            // If the page is one we want the visitor to "continue shopping"
            // to, then save it to visitor's Session
            for (int i = 0; i < catalogPages.GetLength(0); i++)
                if (String.Compare(catalogPages[i], currentLocation, true) == 0)
                {
                    // save the current location
                    Session["LastVisitedCatalogPage"] = Request.Url.ToString();
                    // stop the for loop from continuing
                    break;
                }
        }
    }
    protected void FillMainMenu()
    {
        //if (TreeViewMainMenu == null)
        //    TreeViewMainMenu = new TreeView();
        TreeViewMainMenu.Nodes.Clear();
        DataLoadOptions opts = new DataLoadOptions();
        opts.LoadWith<Sections>(d => d.id_section);
        PresentsDataClassesDataContext db = new PresentsDataClassesDataContext();
        var depts = (from dep in db.Sections
                     select dep);

        foreach (Sections d in depts)
        {
            TreeNode node = new TreeNode(d.name_s.ToLower());
            //node.NavigateUrl = Request.Path;
            //node.ImageUrl = "Styles/Images/ArrowMenu2.png";
            //depItem.Selectable = false;
            TreeViewMainMenu.Nodes.Add(node);
            foreach (Categories cat in d.Categories)
            {
                TreeNode tn = new TreeNode("     " + cat.name_c, cat.id_category.ToString());
                tn.NavigateUrl = "Presents.aspx" + "?category=" + cat.id_category;
                node.ChildNodes.Add(tn);
            }
        }
        //TreeViewMainMenu.CollapseAll();
        TreeViewMainMenu.ExpandAll();
    }
    public bool EnforceSSL
    {
        get
        {
            if (ViewState["enforceSSL"] != null)
            {
                return (bool)ViewState["enforceSSL"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["enforceSSL"] = value;
        }
    }
}
